package com.zlp.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zlp.common.base.BaseApiService;
import com.zlp.common.base.BaseResponse;
import com.zlp.common.constants.Constants;
import com.zlp.common.core.bean.BeanToUtils;
import com.zlp.common.core.utils.DateUtil;
import com.zlp.common.core.utils.MD5Util;
import com.zlp.member.dao.UserDoMapper;
import com.zlp.member.entity.UserDo;
import com.zlp.member.feign.VerificaCodeServiceFeign;
import com.zlp.member.input.dao.UserInpDTO;
import com.zlp.member.service.MemberRegisterService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberRegisterServiceImpl extends BaseApiService<JSONObject> implements MemberRegisterService{


	@Autowired
	private VerificaCodeServiceFeign verificaCodeServiceFeign;
	@Autowired
	private UserDoMapper userMapper;

	@Transactional
	public BaseResponse<JSONObject> register(@RequestBody UserInpDTO userInpDTO, String registCode) {
		// 1.参数验证
		 String userName = userInpDTO.getUserName();
		 if (StringUtils.isEmpty(userName)) {
		 return setResultError("用户名称不能为空!");
		 }
		String mobile = userInpDTO.getMobile();
		if (StringUtils.isEmpty(mobile)) {
			return setResultError("手机号码不能为空!");
		}

		String password = userInpDTO.getUserPwd();
		if (StringUtils.isEmpty(password)) {
			return setResultError("密码不能为空!");
		}
		//  2.验证码注册码是否正确-->会员调用微信接口实现注册码验证
		/*BaseResponse<JSONObject> verificaWeixinCode = verificaCodeServiceFeign.verificaWeixinCode(mobile, registCode);
		if (!verificaWeixinCode.getCode().equals(Econstants.HTTP_RES_CODE_200)) {
			return setResultError(verificaWeixinCode.getMsg());
		}*/
		// 3.对用户的密码进行加密 // MD5 可以解密 暴力破解
		String newPassword = MD5Util.MD5(password);
		userInpDTO.setUserPwd(newPassword);
		// 4.调用数据库插入数据 将请求的dto参数转换DO
		UserDo userDo = BeanToUtils.dtoToDo(userInpDTO, UserDo.class);
		userDo.setCreateBy(userDo.getUserId());
		userDo.setCreateTime(DateUtil.getDateTime());
		userDo.setIsAvailability(Constants.IS_AVAILABILITY_0);//正常
		return userMapper.insert(userDo) > 0 ? setResultSuccess("注册成功") : setResultError("注册失败!");

	}

}
